<?php

namespace App\Service\Activity\v1_3_0;

use App\Constants\ErrorCode;
use App\Helpers\AliyunUtil;
use App\Model\Act\SigninLog;
use App\Model\CoinLog;
use App\Model\Gift;
use App\Model\Service\ConsumeService;
use App\Model\UserPackBag;
use App\Service\BaseService;

/**
 * 签到
 */
class SignInService extends BaseService
{
    public function handle()
    {
        $userId = $this->getUserId();
        $checkLast = SigninLog::getLastItem($userId);
        if (SigninLog::checkToday($checkLast)) {
            return $this->setError(ErrorCode::SIGNIN_TODAY, '你今天已经签到过了哦～');
        }
        //签到
        $signinDay  = SigninLog::getSignInDay($checkLast) + 1;
        $giftConf = SigninLog::getRewardGiftConf();
        $signInfo = $signinDay <= 7 && isset($giftConf[$signinDay]) ? $giftConf[$signinDay] : $giftConf[1];
        //执行签到
        $result = SigninLog::doSignIn($userId, $checkLast, $signInfo['gift_id'], $signInfo['gift_num']);
        //礼物信息
        $giftInfo = Gift::getInfo($signInfo['gift_id']);
        switch ($giftInfo['type']) {
                //金币礼物
            case Gift::TYPE_COIN:
                //赠送金币
                $signCoin = $giftInfo['coin'] * $signInfo['gift_num'];
                ConsumeService::gainIncome($userId, $signCoin, CoinLog::FROM_SIGNIN);
                break;
                //背包礼物
            case Gift::TYPE_PACKBAG:
                UserPackBag::addRecord($userId, $giftInfo['id'], $signInfo['gift_num']);
                break;
        }

        return [
            'result' => $result,
            'reward_icon' => AliyunUtil::imageResize(AliyunUtil::IMG_HOST_V2 . $giftInfo['icon'], 200, 200),
            'reward_tip' => "恭喜获得{$signInfo['gift_num']}{$giftInfo['name']}",
            'extra_tip' => "成功邀请好友后，{$signInfo['gift_num']}金币将翻2倍！（今天24点前有效）"
        ];
    }
}
